﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using SMSPDULib;

namespace SMSRouter
{
    public class SmsManager
    {
        SMSEngine smsEngine = new SMSEngine();
        public SerialPort port;
        public bool modemOnline = false;
        public bool SendMessage(Message mes)
        {
            if (modemOnline)
            {
                try
                {
                    SMS.SMSEncoding encoding = SMS.SMSEncoding.UCS2;
                    if (smsEngine.sendMsg(this.port, mes.To, mes.text, encoding))
                    {
                        return true;
                        //MessageBox.Show("Message has sent successfully");
                        //this.statusBar1.Text = "Message has sent successfully";
                    }
                    else
                    {
                        return false;
                        //MessageBox.Show("Failed to send message");
                        //this.statusBar1.Text = "Failed to send message";
                    }

                }
                catch (Exception ex)
                {
                    //ErrorLog(ex.Message);
                    return false;
                }
            }
            return false;
        }
        public List<Message> getIncomingMessages()
        {
            if (modemOnline)
            {
                try
                {
                    //count SMS 
                    int uCountSMS = smsEngine.CountSMSmessages(this.port);
                    if (uCountSMS > 0)
                    {

                        string strCommand = "AT+CMGL=4"; //"AT+CMGL=\"ALL\"";
                        //strCommand = "AT+CMGL=0"; //\"REC UNREAD\"";
                        //strCommand = "AT+CMGL=3";//\"STO SENT\"";
                        //strCommand = "AT+CMGL=2";//\"STO UNSENT\"";

                        // If SMS exist then read SMS
                        //.............................................. Read all SMS ....................................................
                        List<Message> resultingList;
                        resultingList = smsEngine.ReadSMS(this.port, strCommand);
                        if (resultingList == null)
                            resultingList = new List<Message>();
                        return resultingList;

                    }
                    else
                    {
                        return new List<Message>();
                    }
                }
                catch (Exception ex)
                {
                    return new List<Message>();
                }
            }

            // Get fake empty list
            return new List<Message>();
        }
        public bool ConnectModem(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
        {
            port = smsEngine.OpenPort(p_strPortName, p_uBaudRate, p_uDataBits, p_uReadTimeout, p_uWriteTimeout);
            if (port == null)
                return false;
            modemOnline = true;
            return true;
        }
        public void EnterPinCode(string pinCode)
        {
            smsEngine.EnterPinCode(port, pinCode);
        }
        public bool CheckPinStatus()
        {
            return smsEngine.CheckPinStatus(port);
        }
        public void ClosePort()
        {
            try
            {
                smsEngine.ClosePort(port);
            }
            catch { }
        }


    }
}
